package com.daxt.controller.index;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.daxt.base.common.util.RedisUtil;
import com.daxt.base.common.util.UserInfoUtil;
import com.daxt.common.annotations.LoginAuth;
import com.daxt.common.annotations.LoginUser;
import com.daxt.model.dic.UserClassifyType;
import com.daxt.model.service.base.result.UserInfo;
import com.daxt.model.service.base.vo.UserClassify;
import com.daxt.service.UserService;
import com.daxt.utils.BaseUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.daxt.common.result.Result;
import com.daxt.common.result.ResultCode;
import com.daxt.common.result.ResultUtil;
import com.daxt.mapper.sys.ArchivesFondsMapper;
import com.daxt.mapper.sys.ArchivesTypeMapper;
import com.daxt.model.service.archives.result.ArchivesTree;
import com.daxt.model.service.archives.vo.ArchivesFonds;
import com.daxt.model.service.archives.vo.ArchivesType;
import com.daxt.service.FondsListService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

// 0 全宗  1档案类型  2数据节点
@Api(tags = "全宗字典 ")
@RestController
public class FondsListController {
	
	@Autowired
	private ArchivesFondsMapper archivesFondsMapper;
	@Autowired
	private ArchivesTypeMapper archivesTypeMapper;
	@Autowired
	private FondsListService fondsListService;

	@Autowired
	private UserInfoUtil userInfoUtil;
	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private UserService userService;

	@ApiOperation(value = "全宗树  0 全宗  1档案类型 2数据节点(0 字段节点   1分类节点)", notes = "")
	@GetMapping("/select/tree")
	public Result<List<ArchivesTree>> getTree(@RequestParam(value = "t", required = false)String t) {
		List<ArchivesTree> tree = fondsListService.getTree(t);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", tree);
	}

	/**
	 * @description: 修改  增加用户数据权限控制
	 *
	 * @author: lig
	 * @date: 2021-06-15
	 */
	@ApiOperation(value = "（数据权限）获取全宗门类树  0 全宗  1档案类型 2数据节点(0 字段节点   1分类节点)", notes = "")
	@GetMapping("/select/fondsTree")
	@LoginAuth()
	public Result<List<ArchivesTree>> getFondsTree(@ApiIgnore @LoginUser UserInfo loginUser, @ApiParam("1 审批  2回收站")@RequestParam(value = "t", required = false)String t
										                  ,@ApiParam("1 卷  0 件")@RequestParam(value = "model", required = false)String model
														  ,@ApiParam("全宗id")@RequestParam(value = "fondsId", required = false)String fondsId ) {
		List<ArchivesTree> tree = fondsListService.getFondsTree(t,loginUser,model,fondsId);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", tree);
	}

	@ApiOperation(value = "获取全宗门类树  0 全宗  1档案类型 2数据节点(0 字段节点   1分类节点)", notes = "")
	@GetMapping("/select/getFondsTreeAll")
	public Result<List<ArchivesTree>> getFondsTreeAll(@ApiParam("1 审批  2回收站")@RequestParam(value = "t", required = false)String t) {
		List<ArchivesTree> tree = fondsListService.getFondsTree(t);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", tree);
	}

	@ApiOperation(value = "获取门类下的数据树  0 全宗  1档案类型 2数据节点(0 字段节点   1分类节点)", notes = "")
	@GetMapping("/select/dataTree/{fondsId}/{typeId}/{classId}")
	public Result<List<ArchivesTree>> getDataTree(@PathVariable("fondsId")String fondsId,
			@PathVariable("typeId")String typeId,
			@PathVariable("classId")String classId,
			HttpServletRequest request,
			@RequestParam(value = "t", required = false)String t) {
		UserInfo loginInfo = userInfoUtil.getLoginInfo(UserInfo.class, redisUtil, request);
		if (loginInfo == null) {
			return ResultUtil.data(ResultCode.NOT_LOGIN.getCode(), ResultCode.NOT_LOGIN.getMes(), null);
		}
		List<ArchivesTree> tree = fondsListService.getDataTree(t,fondsId,typeId,classId,loginInfo);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", tree);
	}

	@ApiOperation(value = "获取全宗下拉列表,有权限", notes = "")
	@GetMapping("/select/fonds")
	public Result<List<ArchivesFonds>> getFondsList(HttpServletRequest request) {

		UserInfo loginInfo = userInfoUtil.getLoginInfo(UserInfo.class, redisUtil, request);
		if (loginInfo == null) {
			return ResultUtil.data(ResultCode.NOT_LOGIN.getCode(), ResultCode.NOT_LOGIN.getMes(), null);
		}
		//超级管理员可以看到所有全宗的数据，其它管理员只能看到自己本部门全宗的数据
		String userName= loginInfo.getUsername();
		String role = loginInfo.getRole();

		//获取数据权限 lig 2021-06-18
		List<UserClassify> dataPrivilege = userService.getUserClassify(loginInfo);
		QueryWrapper<ArchivesFonds> queryWrapper=new QueryWrapper<ArchivesFonds>();

		//增加权限条件 lig 2021-06-18
		if(BaseUtil.listIsNotNullElement(dataPrivilege) && ("admin".equals(userName) || "超级管理员".equals(role)) ){
			List<Long> afIds=new ArrayList<>();//全宗ids
			for (UserClassify ucBean : dataPrivilege) {
				if(ucBean.getType() == UserClassifyType.LEVEL_1.getKey())afIds.add(ucBean.getClassifyId());
			}
			queryWrapper.in("id",afIds);
		}else {
			String fondsId=loginInfo.getFondsId();
			queryWrapper.eq(StringUtils.isNotEmpty(fondsId),"id",fondsId);
		}
		queryWrapper.orderByAsc("sort");
		List<ArchivesFonds> fonds = archivesFondsMapper.selectList(queryWrapper);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", fonds);
	}

	@ApiOperation(value = "获取门类列表", notes = "")
	@GetMapping("/select/archivesType")
	public Result<List<ArchivesType>> getArchivesTypeList(@ApiParam("全宗号") @RequestParam("fondsId") String fondsId) {
		QueryWrapper<ArchivesType> queryWrapper = new QueryWrapper<ArchivesType>();
		queryWrapper.eq("fondsId", fondsId);
		List<ArchivesType> fonds = archivesTypeMapper.selectList(queryWrapper);
		if (fonds != null && fonds.size() > 0)
			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", fonds);
		else
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "", null);
	}


	/**
	 * @description: 修改  增加用户数据权限控制
	 *
	 * @author: lig
	 * @date: 2021-06-15
	 */
	@ApiOperation(value = "全宗类型审批数量", notes = "")
	@GetMapping("/select/fondsTypeExamineCount")
	public Result<List<Map>> fondsTypeExamineCount(@ApiParam("1 审批  2回收站")@RequestParam(value = "t", required = false)String t,@ApiParam("全宗ID")@RequestParam(value = "fondsId", required = true)String fondsId, HttpServletRequest request) {

		UserInfo loginInfo = userInfoUtil.getLoginInfo(UserInfo.class, redisUtil, request);
		if (loginInfo == null) {
			return ResultUtil.data(ResultCode.NOT_LOGIN.getCode(), ResultCode.NOT_LOGIN.getMes(), null);
		}

		List<Map> list = fondsListService.fondsTypeExamineCount(t,loginInfo,fondsId);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", list);
	}


	@ApiOperation(value = "获取全宗门类树，无权限  0 全宗  1档案类型 2数据节点(0 字段节点   1分类节点)", notes = "")
	@GetMapping("/select/getFondsTreeAllNew")
	public Result<List<ArchivesTree>> getFondsTreeAllNew(@RequestParam(value = "model",required = false)String model
														, HttpServletRequest request) {

		String fondsId=null;
		UserInfo loginInfo = userInfoUtil.getLoginInfo(UserInfo.class, redisUtil, request);
		if (loginInfo == null) {
			return ResultUtil.data(ResultCode.NOT_LOGIN.getCode(), ResultCode.NOT_LOGIN.getMes(), null);
		}
		if("admin".equals(loginInfo.getUsername()) || "超级管理员".equals(loginInfo.getRole())){

		}else {
			fondsId=loginInfo.getFondsId();
		}
		List<ArchivesTree> tree = fondsListService.getFondsTreeUpgrade(fondsId,model);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", tree);
	}

	@ApiOperation(value = "获取全宗门类树，有权限  0 全宗  1档案类型 2数据节点(0 字段节点   1分类节点)", notes = "")
	@GetMapping("/select/getFondsTreePower")
	public Result<List<ArchivesTree>> getFondsTreeAllNewPower(@RequestParam(value = "model",required = false)String model
			,HttpServletRequest request) {
		UserInfo loginInfo = userInfoUtil.getLoginInfo(UserInfo.class, redisUtil, request);
		if (loginInfo == null) {
			return ResultUtil.data(ResultCode.NOT_LOGIN.getCode(), ResultCode.NOT_LOGIN.getMes(), null);
		}
		List<ArchivesTree> tree = fondsListService.getFondsTreeUpgrade(loginInfo,model);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", tree);
	}
}
